package service

import (
	"teacher-helper/common"
	"teacher-helper/dao"
	"teacher-helper/models"
	"teacher-helper/models/request"
)

// 登录验证
func LoginCheck(username string, password string) (user *models.SysUser, msg string, isPass bool) {
	password = common.Sha1(password)
	user, ok := CheckUserExist(username, password)
	if !ok {
		return user, "用户或密码不正确", false
	}
	return user, "登录成功", true
}

// 验证用户是否存在
func CheckUserExist(username string, password string) (user *models.SysUser, ok bool) {
	user = new(models.SysUser)
	dao.DB.Where(&models.SysUser{Username:username, Password:password, State:true}).Preload("Role").Preload("Major").First(&user)
	if user.ID > 0 {
		dao.DB.Where(&models.SysClass{CounselorID:user.ID}).Find(&user.ClassList)
		return user, true
	}
	return
}

func ResetPassword(user_form request.UserPasswordForm) (err error) {
	err = user_form.Validation()
	if err != nil {
		return err
	}
	// 数据库更新密码
	password := common.Sha1(user_form.Password)
	new_password := common.Sha1(user_form.NewPassword)
	user := models.SysUser{Username:user_form.Username, Password:password}
	return dao.DB.Model(&models.SysUser{}).Where(&user).Update("password", new_password).Error
}


func GetUserInfoList(queryInfo request.QueryInfo) (users []models.SysUser, total int,  err error) {
	if queryInfo.UserType == "counselor" {
		err = dao.DB.Where("role_id = ?", 2).Find(&users).Count(&total).Error
		return
	}
	if queryInfo.Query == "" {
		if err = dao.DB.Model(&models.SysUser{}).Count(&total).Error; err != nil{
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum-1)*queryInfo.PageSize).Preload("Role").Preload("Major").Find(&users).Error
	}else{
		if err = dao.DB.Model(&models.SysUser{}).Where("username like ?", queryInfo.Query + "%").Count(&total).Error; err != nil{
			return
		}
		err = dao.DB.Where("username like ?", queryInfo.Query + "%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum-1)*queryInfo.PageSize).Preload("Role").Preload("Major").Find(&users).Error
	}
	return
}

func ChangeUserState(user request.StateForm) error {
	return dao.DB.Model(&models.SysUser{}).Where("id = ?", user.ID).Update("state", user.State).Error
}

func UpdateUserRole(user request.UserRoleForm) (err error){
	var role models.SysRole
	err = dao.DB.Where(&models.SysRole{RoleId:user.RoleId}).First(&role).Error
	return dao.DB.Model(&models.SysUser{}).Where("id = ?", user.UserId).Update("role_id", user.RoleId).Error
}

func AddOneUser(user models.SysUser) error {
	user.Major = models.SysMajor{}
	user.Role = models.SysRole{}
	user.Password = common.Sha1("000000")	// 默认密码为000000
	return dao.DB.Create(&user).Error
}

func UpdateUser(user models.SysUser) error {
	user.Major = models.SysMajor{}
	user.Role = models.SysRole{}
	return dao.DB.Model(&models.SysUser{}).Where("id = ?", user.ID).Updates(&models.SysUser{Username:user.Username, RoleId:user.RoleId, MajorID:user.MajorID,Grades:user.Grades}).Error
}

func DeleteUser(id int) error {
	return dao.DB.Unscoped().Where("id = ?", id).Delete(&models.SysUser{}).Error
}